//
//  QKTimeGroupModel.m
//  testTool
//
//  Created by Su on 2018/12/10.
//  Copyright © 2018 com.qiankun. All rights reserved.
//

#import "QKTimeGroupModel.h"

@implementation QKTimeGroupModel

+ (NSMutableArray *)getData {
    
    NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"groupjson.json" ofType:nil]];
    
    if (data == nil) {
        return nil;
    }
    NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    NSMutableArray *resultArr = [NSMutableArray array];
    
    for (NSDictionary *dict in array) {
        
        NSLog(@"%@",dict);
        QKTimeGroupModel *model = [QKTimeGroupModel mj_objectWithKeyValues:dict];
        [resultArr addObject:model];
    }
    return resultArr;
}


-(void)testDict{
    
    NSMutableArray *modelArray = [QKTimeGroupModel getData];
    NSString *pervDate = @"temp";
    NSUInteger sectionNumber = 0;
    
    for (int i = 0; i < modelArray.count; i++) {
        QKTimeGroupModel *model = modelArray[i];
        if ([model.date_str isEqualToString:pervDate]) {
            NSMutableArray *arr = (NSMutableArray *)modelArray[sectionNumber];
            [arr addObject:model];
            [modelArray replaceObjectAtIndex:sectionNumber withObject:arr];
        }else{
            pervDate = model.date_str;
            NSMutableArray *arr = [NSMutableArray array];
            [arr addObject:model];
            [modelArray addObject:arr];
            sectionNumber = modelArray.count - 1;
        }
    }
    
}
#pragma mark  ==== 处理后台数据结构
- (void)handleArraysAction {
    NSMutableArray *timeArr = [NSMutableArray array];
    __weak typeof(self) weakSelf = self;
    
    [self.requestDataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        QKTimeGroupModel* headModel = obj;
        [timeArr addObject:headModel.date_str];
    }];
    
    NSSet *set = [NSSet setWithArray:timeArr];//使用asset把timeArr的日期去重
    NSArray *userArray = [set allObjects];//去重后的时间数组
    
    
    //遍历userArray   按照userArray里的时间分成几个组每个组都是空的数组
    //    self.dataSource = [NSMutableArray array];
    [userArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSMutableArray *arr = [NSMutableArray array];
        [weakSelf.dataSource addObject:arr];
    }];
    
    //遍历self.requestDataArray取其中每个数据的日期看看与userArray里的那个日期匹配就把这个数据装到sectionArray对应的组中
    [self.requestDataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        QKTimeGroupModel* headModel = obj;
        for (NSString *timeStr in userArray) {
            if ([timeStr isEqualToString:headModel.date_str]) {
                NSMutableArray *emptyArr = [weakSelf.dataSource objectAtIndex:[userArray indexOfObject:timeStr]];//取到对应的空数组
                [emptyArr addObject:headModel];
            }
        }
    }];
    
}

@end
